/*
   FieldVis - A Tool for Visualizing Astrophysical Magnetohydrodynamic Flow
   
   Copyright (C) 2006 - 2008     FieldVis Development Team
  
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   File        : DataSet.h
   Author      : Jeremy Moc  (mocj@msi.umn.edu)

   Description : Data structures to hold the file data.  These objects are never
                  accessed directly.  All access to the data is controlled by the
                  objects in Field.{h/cpp}.  The set manager here is a singleton.
*/

#ifndef DATASET_H
#define DATASET_H

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>

#include "Mutex.h"

///////////////////////////////////////////////////////////////////////////////

class DataSlice
{
public:

   DataSlice(std::ifstream &f, const int x, const int y, const int z);
   
   ~DataSlice()   { delete [] slice; }

   float Get(const int x, const int y) const { return (slice[x + xSize * y]); }

protected:
   
   int xSize;   
   float *slice;
};

///////////////////////////////////////////////////////////////////////////////

class DataSet
{
public:

   DataSet(const std::string &filename, const int x, const int y, const int z);
   
   ~DataSet();

   float Get(const int x, const int y, const int z);

protected:

   static Mutex mutex;
   int xSize, ySize, zSize;
   std::ifstream file;
   std::vector<DataSlice*> *vi;
};

///////////////////////////////////////////////////////////////////////////////

class DataSetManager
{
public:
	
   ~DataSetManager();

   DataSet *Manage(const std::string filename, const int x, const int y, const int z);

   static DataSetManager &Instance()   { return (manager); }

protected:
	
   DataSetManager()  { }

   static Mutex mutex;
   static DataSetManager manager;
   std::map<std::string, DataSet*> theMap;
};

#endif
